package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.listener.MtsxfExcelListener;
import cn.stylefeng.guns.sys.core.listener.RxjsxfExcelListener;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyDysxf;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMtsxf;
import cn.stylefeng.guns.sys.modular.system.entity.BtyRxjsxf;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyRxjsxfMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyMtsxfParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyRxjsxfParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyMtsxfResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyRxjsxfResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyRxjsxfService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * 控制器
 *
 * @author liuy
 * @Date 2022-02-23 16:03:19
 */
@Controller
@RequestMapping("/btyRxjsxf")
public class BtyRxjsxfController extends BaseController {

    private String PREFIX = "/modular/system/btyRxjsxf";

    @Autowired
    private BtyRxjsxfService btyRxjsxfService;
    @Resource
    private BtyRxjsxfMapper btyRxjsxfMapper;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2022-02-23
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyRxjsxf.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2022-02-23
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyRxjsxf_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2022-02-23
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyRxjsxf_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2022-02-23
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyRxjsxfParam btyRxjsxfParam) {
        this.btyRxjsxfService.add(btyRxjsxfParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2022-02-23
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyRxjsxfParam btyRxjsxfParam) {
        this.btyRxjsxfService.update(btyRxjsxfParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2022-02-23
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyRxjsxfParam btyRxjsxfParam) {
        this.btyRxjsxfService.delete(btyRxjsxfParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2022-02-23
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyRxjsxfParam btyRxjsxfParam) {
        BtyRxjsxf detail = this.btyRxjsxfService.getById(btyRxjsxfParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2022-02-23
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyRxjsxfParam btyRxjsxfParam) {
        return this.btyRxjsxfService.findPageBySpec(btyRxjsxfParam);
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUsers(@RequestParam("file") MultipartFile file, BtyRxjsxfParam rxjsxfParam) {
        JsonResult jsonResult = new JsonResult();
        if(ToolUtil.isEmpty(rxjsxfParam.getZdrq())){
            jsonResult.setState(0);
            jsonResult.setErrmsg("请先选择制单日期！");
            return jsonResult;
        }
        //导入之前删掉之前的数据
        btyRxjsxfMapper.delete(new QueryWrapper<>());
        try {
            RxjsxfExcelListener rxjsxfExcelListener=new RxjsxfExcelListener(btyRxjsxfService);
            EasyExcel.read(file.getInputStream(), BtyRxjsxf.class, rxjsxfExcelListener).sheet(0).doRead();
            //导入成功之后按大区生成凭证
            String zdrq=rxjsxfParam.getZdrq();
            List<BtyRxjsxfResult>list=btyRxjsxfService.rxjList();
            for (BtyRxjsxfResult rxjsxfResult : list) {
                BigDecimal sxf=rxjsxfResult.getSxf();
                String bh=rxjsxfResult.getRegion();
                String yaoma=rxjsxfResult.getYaoma();
                String dm=rxjsxfResult.getDm();
                RestTemplate restTemplate = new RestTemplate();
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
                headers.add("system", "001"); // 系统编码
                headers.add("usercode", "15253172037"); // 用户
                headers.add("password", "514576f58a89767b46c1b44767a08f77");
                headers.add("Accept", MediaType.APPLICATION_JSON.toString());

                StringBuilder sb=new StringBuilder();
                sb.append("{\n" +
                        "    \"voucher\":[\n" +
                        "        {\n" +
                        "            \"details\":[");

                //现金手续费凭证
                sb.append("                {\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+sxf+"\",\n" +
                        "                    \"explanation\":\"现金手续费\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"660306\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }," +
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "\t\t\t\t\t\t{\n" +
                        "                            \"checktypecode\":\"7\",\n" +
                        "                            \"checkvaluecode\":\"0038\"\n" +//人民币结算方式编码
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+sxf+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"现金手续费\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"112203\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n");
                sb.append("            ],\n" +
                        "            \"pk_corp\":"+bh+",\n" +
                        "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                        "            \"pk_prepared\":\"15253172037\",\n" +
                        "            \"pk_vouchertype\":\"记账\",\n" +
                        "            \"prepareddate\":\""+zdrq+"\"\n" +
                        "        }\n" +
                        "    ]\n" +
                        "}");
                //System.out.println("sb:"+sb.toString());
                HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
                String url = "http://124.70.28.147:8089/u8cloud/api/gl/voucher/insert";
                String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
                //System.out.println("result:"+result);
                JsonParser jp = new JsonParser();
                //将json字符串转化成json对象
                JsonObject jo = jp.parse(result).getAsJsonObject();
                //获取status对应的值
                String status = jo.get("status").getAsString();

                if(status.equals("falied")){
                    String errormsg = jo.get("errormsg").getAsString();
                    BtyRxjsxf dysxf=btyRxjsxfService.getOne(new QueryWrapper<BtyRxjsxf>().eq("dm",dm));
                    if(ToolUtil.isNotEmpty(dysxf)){
                        dysxf.setErrormsg(errormsg);
                        btyRxjsxfService.updateById(dysxf);
                    }
//
                }else{
                    BtyRxjsxf dysxf=btyRxjsxfService.getOne(new QueryWrapper<BtyRxjsxf>().eq("dm",dm));
                    if(ToolUtil.isNotEmpty(dysxf)){
                        dysxf.setFlag(1);
                        btyRxjsxfService.updateById(dysxf);
                    }
                    //System.out.println("凭证生成成功！");
                }
            }

        }catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return true;
    }

}


